/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl;

import cz.muni.fi.diplomka.datalayer.dao.MessageDao;
import cz.muni.fi.diplomka.datalayer.daoimpl.MessageDaoImpl;
import cz.muni.fi.diplomka.datalayer.entity.MessageEntity;
import cz.muni.fi.diplomka.servicelayer.dto.Message;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MessageManager;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

/**
 *
 * @author Jakub Kutil
 */
public class MessageManagerImpl implements MessageManager {

    private MessageDao messageDao = new MessageDaoImpl();
    private Mapper mapper = new DozerBeanMapper();

    @Override
    public void addMessage(Message message) {
        if (message == null) {
            throw new IllegalArgumentException("Message can not be null.");
        }

        messageDao.addMessage(mapper.map(message, MessageEntity.class));
    }

    @Override
    public void editMessage(Message message) {
        if (message == null) {
            throw new IllegalArgumentException("Message can not be null.");
        }

        messageDao.editMessage(mapper.map(message, MessageEntity.class));
    }

    @Override
    public void deleteMessage(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id can not be null.");
        }
        messageDao.deleteMessage(id);
    }

    @Override
    public Message findMessage(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id can not be null.");
        }
        return mapper.map(messageDao.findMessage(id), Message.class);

    }

    @Override
    public List<Message> getAllReceivedMessagesOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("PlayerId can not be null.");
        }

        List<Message> messageList = new ArrayList<>();
        for (MessageEntity messageEntity : messageDao.getAllReceivedMessagesOfPlayer(playerId)) {
            messageList.add(mapper.map(messageEntity, Message.class));
        }

        return messageList;
    }

    @Override
    public List<Message> getAllSentMessagesOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("PlayerId can not be null.");
        }

        List<Message> messageList = new ArrayList<>();
        for (MessageEntity messageEntity : messageDao.getAllSentMessagesOfPlayer(playerId)) {
            messageList.add(mapper.map(messageEntity, Message.class));
        }

        return messageList;
    }

    @Override
    public int getCountOfUnreadReceivedMessagesOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("PlayerId can not be null.");
        }
    return messageDao.getCountOfUnreadReceivedMessagesOfPlayer(playerId);
    }
}
